Page({

  /**
   * 页面的初始数据
   */
  data: {
        list: [
        //   { name: '吃饭', isDone: true},
        //   { name: '睡觉', isDone: false},
        //   { name: '打豆豆', isDone: false },
        ],
        name: '',
        count:0,
  },

    input (e) {
        console.log(e);
        let name = e.detail.value.trim()
        if (!name) {
            wx.showToast({
                title: '输入不能为空',
                icon: 'none',
            })
            return
        }
        let obj = {
            name:name,
            isDone: false,
        }
        let  {list,count} = this.data
        list.push(obj)
        this.setData({
            list,
            name: '',
            count: ++count
        }, () => {
            console.log('操作成功')
            wx.showToast({
                title: '操作成功',
                icon: 'success',
              })
        })
        wx.setStorageSync('list',list)
    },
    toggle (e) {
        let index = e.currentTarget.dataset.index
        console.log(index);
        let { list, count } = this.data
        list[index].isDone = !list[index].isDone
        list[index].isDone ? count-- : count++
        this.setData({
            list,
            count
        })
        wx.setStorageSync('list',list)
    },
    del (e) {
        let index= e.currentTarget.dataset.index  
        let {list,count} = this.data
        let [temp] = list.splice(index,1)
        !temp.isDone ? count--:null
        this.setData({
            list,
            count
        })
        wx.setStorageSync('list',list)
    },
    toggleAll () {
        let {list,count} = this.data
        let flag = list.every(item => item.isDone)
        count =  flag ? list.length:0
        list.forEach(item =>item.isDone = !flag)
        this.setData({
            list,
            count
        })
        wx.setStorageSync('list',list)
    },
    clear () {
        console.log('clear')
        let {list} = this.data
        list =  list.filter(item=>item.isDone == false)
        this.setData({
            list
        })
        wx.setStorageSync('list',list)
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let list = wx.getStorageSync('list')
      let count  = 0
      list.forEach(item => {
          if(!item.isDone) count++
      })
      this.setData({
          list,
          count
      })
      
      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})